const ipc = require('electron').ipcRenderer;
const { dialog } = require('electron').remote;
const BrowserWindow = require('electron').remote.BrowserWindow
const path = require('path')
const XLSX = require('xlsx');
const url = require('url');
const planetable = document.getElementById("plan_table");

require('../css/index.css');
import PlaneTable from './PlaneTable';
class App {
    constructor() {
        this.filebtn = document.getElementById("filebtn");
        this.prew_btn = document.getElementById("pre_btns");
        this.huichaolv_btn = document.getElementById("prew_huichaolv");
        this.trans_btn = document.getElementById("prew_trans");


        this.palneTable = new PlaneTable();


        let configObj = {
            'A2': 'serial_number',
            'B2': 'batch_number',
            'C2': 'processing_enterprise',
            'D2': 'product_name',
            'E2': 'processing_mode',
            'F2': 'production_year',
            'G2': 'weight',
            'H2': 'production_number',
            'I2': 'lab',
            'J2': 'packet_number',
            'L2': 'remarks'
        }
        this.filedMap = this.objToStrMap(configObj);
        this.initEvent();


    }
    initEvent() {
        this.filebtn.addEventListener('click', () => {
            dialog.showOpenDialog({
                title: "选择数据文件",
                properties: ['openFile'],
                filters: [{ name: "xlsx", extensions: ["xls", "xlsx"] }]
            }, (files) => {
                if (files !== undefined) {
                    let data = XLSX.readFile(files[0])
                    this.parseTable(data);
                }
            });
        });
        this.huichaolv_btn.addEventListener('click', () => {
            let data = this.palneTable.getData();
            localStorage.setItem('huichaolv', JSON.stringify(data));
            const modalPath = path.join('file://', window.dirname, './src/page/preview_huichaolv.html');

            let win = new BrowserWindow({ width: 800, height: 800 });


            // win.webContents.openDevTools();
            win.on('close', function() { win = null })
            win.loadURL(modalPath);
            win.show();
        });
        this.trans_btn.addEventListener('click', () => {
            let data = this.palneTable.getData();
            localStorage.setItem('trans', JSON.stringify(data));
            const modalPath = path.join('file://', window.dirname, './src/page/transfer_card.html');

            let win = new BrowserWindow({ width: 800, height: 800 });


            // win.webContents.openDevTools();
            win.on('close', function() { win = null })
            win.loadURL(modalPath);
            win.show();
        });
    }
    parseTable(data) {
        let tableData = (data.Sheets[data.SheetNames[0]]);
        let tabelTitle = tableData.A1.v;
        delete tableData.A1; //已经获取到了预约时间 直接删除

        let dataMap = this.objToStrMap(tableData);
        // console.log(data);
        // console.log(tableData);
        // console.log(dataMap);
        let tableHead = [];
        let resultData = [];
        const datas = dataMap.entries();

        const cols = this.filedMap.size;
        for (var i = 0; i < cols; i++) {
            let data = datas.next().value;
            var head = { field: this.filedMap.get(data[0]), title: data[1].v, align: 'center', width: 100 };
            tableHead.push(head);
        }
        //解析数据
        const datasArray = [...dataMap.values()];
        const dataField = [...this.filedMap.values()];
        // console.log(dataField);
        let j = 0;
        let result = {};
        for (var k = 11; k < dataMap.size; k++) {
            let data = datasArray[k];

            result[dataField[j]] = data.v;

            j++;
            if (j % 9 === 0) {
                result[dataField[9]] = '';
                result[dataField[10]] = '';
                resultData.push(result);
                j = 0;
                result = {};
            }

        }
        //     console.log(resultData);
        let table_title = document.getElementById("plan_table_title");
        table_title.innerHTML = tabelTitle;
        this.palneTable.setData(tableHead, resultData);
        this.prew_btn.style.display = 'block';


    }

    objToStrMap(obj) {
        let strMap = new Map();
        for (let k of Object.keys(obj)) {
            if (k.indexOf('!') > -1) {
                continue;
            }
            strMap.set(k, obj[k]);
        }
        return strMap;
    }



}

export default App;
var a = new App();
window.app = a;